import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useTokenStore } from '../store/token';

const baseURL = '/api';
const instance = axios.create({
    baseURL: baseURL
})

instance.interceptors.request.use(
    (config)=>{
        const tokenStore = useTokenStore();
        if(tokenStore.token){
            config.headers.Authorization = tokenStore.token;
        }
        return config
    },
    (err)=>{
        Promise.reject(err)
    }
)

import router from '../router'

instance.interceptors.response.use(
    result=>{
        if(result.data.code === 200){
            return result.data;
        }

        ElMessage.error(result.data.msg?result.data.msg: '服务异常，请稍后再试')
        return Promise.reject(result.data)
    },
    err=>{
        if(err.response.status === 401){
            ElMessage.error('请先登录')
            router.push('login')
        }else{
            ElMessage.error('服务异常，请稍后再试')
            return Promise.reject(err)
        }
    }
)

export default instance